package com.cwf.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cwf.common.core.controller.BaseController;
import com.cwf.common.entity.http.R;
import com.cwf.common.enums.IsDeleteEnum;
import com.cwf.framework.aspectj.annotation.MyPage;
import com.cwf.system.dao.ApiManagementMapper;
import com.cwf.system.entity.ApiManagement;
import com.cwf.system.entity.Role;
import com.cwf.system.service.ApiManagementService;
import com.cwf.system.vo.ValidGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


import java.math.BigInteger;
import java.util.List;

@RestController
@Validated
@RequestMapping("/system/api/manage")
public class ApiManagementController extends BaseController {

    @Autowired
    ApiManagementMapper apiManagementMapper;

    @Autowired
    ApiManagementService apiManagementService;






    /**
     * 添加api
     * @param apiManagement
     * @return
     */
    @PostMapping("/add/api")
    public R addApi(@RequestBody @Validated(value = ValidGroup.Crud.Create.class) ApiManagement apiManagement){


        return R.insert(apiManagementService.addApi(apiManagement));

    }

    /**
     * 查询api和分页信息
     * @param apiManagement
     * @return
     */
    @GetMapping("/page/list")
    public R searchApi( ApiManagement apiManagement){


        return R.success(apiManagementService.searchApi(apiManagement));
    }

    /**
     * 删除api
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteApi(@PathVariable Long id){

        ApiManagement build = ApiManagement.builder().id(id).isDelete(IsDeleteEnum.OK.getCode()).build();
        return R.delete(apiManagementService.deleteApi(build));
    }

    /**
     * API修改
     * @param apiManagement
     * @return
     */
    @PutMapping("/update")
    public R updateApi(@Validated(value = ValidGroup.Crud.Update.class) @RequestBody ApiManagement apiManagement){

        ApiManagement build = ApiManagement.builder().groups(apiManagement.getGroups())
                .id(apiManagement.getId())
                .path(apiManagement.getPath())
                .des(apiManagement.getDes())
                .request(apiManagement.getRequest()).build();
        return R.update(apiManagementService.updateApi(build));
    }
}
